package top.xiaoqiannb.shapelessdao.procedures;

import top.xiaoqiannb.shapelessdao.network.ShapelessdaoModVariables;
import top.xiaoqiannb.shapelessdao.init.ShapelessdaoModMobEffects;
import top.xiaoqiannb.shapelessdao.init.ShapelessdaoModBlocks;
import top.xiaoqiannb.shapelessdao.ShapelessdaoMod;

import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.util.RandomSource;
import net.minecraft.core.BlockPos;

import java.util.Map;

public class QimenkaipanProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity, ItemStack itemstack) {
		if (entity == null)
			return;
		if (!(entity instanceof LivingEntity _livEnt0 && _livEnt0.hasEffect(ShapelessdaoModMobEffects.QIMEN.get()))) {
			if (((entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ShapelessdaoModVariables.PlayerVariables())).Sect).equals("QimenII")) {
				world.setBlock(BlockPos.containing(x, y + 1, z), ShapelessdaoModBlocks.QIMENZHENFA_II.get().defaultBlockState(), 3);
				{
					int _value = 1;
					BlockPos _pos = BlockPos.containing(x, y + 1, z);
					BlockState _bs = world.getBlockState(_pos);
					if (_bs.getBlock().getStateDefinition().getProperty("animation") instanceof IntegerProperty _integerProp && _integerProp.getPossibleValues().contains(_value))
						world.setBlock(_pos, _bs.setValue(_integerProp, _value), 3);
				}
				ShapelessdaoMod.queueServerWork(20, () -> {
					{
						int _value = 0;
						BlockPos _pos = BlockPos.containing(x, y + 1, z);
						BlockState _bs = world.getBlockState(_pos);
						if (_bs.getBlock().getStateDefinition().getProperty("animation") instanceof IntegerProperty _integerProp && _integerProp.getPossibleValues().contains(_value))
							world.setBlock(_pos, _bs.setValue(_integerProp, _value), 3);
					}
				});
				if (entity instanceof Player _player)
					_player.getCooldowns().addCooldown(itemstack.getItem(), 2000);
				{
					ItemStack _ist = itemstack;
					if (_ist.hurt(1, RandomSource.create(), null)) {
						_ist.shrink(1);
						_ist.setDamageValue(0);
					}
				}
				ShapelessdaoMod.queueServerWork(2000, () -> {
					{
						BlockPos _bp = BlockPos.containing(x, y + 1, z);
						BlockState _bs = Blocks.AIR.defaultBlockState();
						BlockState _bso = world.getBlockState(_bp);
						for (Map.Entry<Property<?>, Comparable<?>> entry : _bso.getValues().entrySet()) {
							Property _property = _bs.getBlock().getStateDefinition().getProperty(entry.getKey().getName());
							if (_property != null && _bs.getValue(_property) != null)
								try {
									_bs = _bs.setValue(_property, (Comparable) entry.getValue());
								} catch (Exception e) {
								}
						}
						world.setBlock(_bp, _bs, 3);
					}
				});
			}
			if (((entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ShapelessdaoModVariables.PlayerVariables())).Sect).equals("QimenI")) {
				world.setBlock(BlockPos.containing(x, y + 1, z), ShapelessdaoModBlocks.QIMENZHENFA_I.get().defaultBlockState(), 3);
				{
					int _value = 1;
					BlockPos _pos = BlockPos.containing(x, y + 1, z);
					BlockState _bs = world.getBlockState(_pos);
					if (_bs.getBlock().getStateDefinition().getProperty("animation") instanceof IntegerProperty _integerProp && _integerProp.getPossibleValues().contains(_value))
						world.setBlock(_pos, _bs.setValue(_integerProp, _value), 3);
				}
				ShapelessdaoMod.queueServerWork(20, () -> {
					{
						int _value = 0;
						BlockPos _pos = BlockPos.containing(x, y + 1, z);
						BlockState _bs = world.getBlockState(_pos);
						if (_bs.getBlock().getStateDefinition().getProperty("animation") instanceof IntegerProperty _integerProp && _integerProp.getPossibleValues().contains(_value))
							world.setBlock(_pos, _bs.setValue(_integerProp, _value), 3);
					}
				});
				if (entity instanceof Player _player)
					_player.getCooldowns().addCooldown(itemstack.getItem(), 2000);
				{
					ItemStack _ist = itemstack;
					if (_ist.hurt(1, RandomSource.create(), null)) {
						_ist.shrink(1);
						_ist.setDamageValue(0);
					}
				}
				ShapelessdaoMod.queueServerWork(2000, () -> {
					{
						BlockPos _bp = BlockPos.containing(x, y + 1, z);
						BlockState _bs = Blocks.AIR.defaultBlockState();
						BlockState _bso = world.getBlockState(_bp);
						for (Map.Entry<Property<?>, Comparable<?>> entry : _bso.getValues().entrySet()) {
							Property _property = _bs.getBlock().getStateDefinition().getProperty(entry.getKey().getName());
							if (_property != null && _bs.getValue(_property) != null)
								try {
									_bs = _bs.setValue(_property, (Comparable) entry.getValue());
								} catch (Exception e) {
								}
						}
						world.setBlock(_bp, _bs, 3);
					}
				});
			}
		}
	}
}
